package com.open.capacity.forum.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.FileInfo;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.CodeEnum;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.forum.dao.ResourcesFileDao;
import com.open.capacity.forum.dto.ResourcesFileDto;
import com.open.capacity.forum.feign.FileService;
import com.open.capacity.forum.feign.UserCenterService;
import com.open.capacity.forum.model.ResourcesFile;
import com.open.capacity.forum.service.ResourcesFileService;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 蒋鑫
 * @version 创建时间 2021/3/1
 */
@Service
public class ResourcesFileServiceImpl extends ServiceImpl<ResourcesFileDao, ResourcesFile> implements ResourcesFileService {

    @Resource
    FileService fileService;

    @Resource
    ResourcesFileDao resourcesFileDao;

    @Resource
    UserCenterService userCenterService;

    @Override
    public Result uploadResources(MultipartFile file, Map<String, Object> map) {
        FileInfo fileInfo = null;
        try {
            fileInfo = fileService.upload(file);
            //上传文件
            fileService.upload(file);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(fileInfo, "上传失败");
        }
        //map对象转换为实体对象
        ResourcesFile resourcesFile = JSON.parseObject(JSON.toJSONString(map), ResourcesFile.class);
        resourcesFile.setUpdtim(new Date());
        resourcesFile.setCretim(new Date());
        resourcesFile.setFiname(fileInfo.getName());
        resourcesFile.setFisize((int) fileInfo.getSize());
        resourcesFile.setFitype(fileInfo.getContentType());
        resourcesFile.setFilurl(fileInfo.getUrl());
        resourcesFile.setFipath(fileInfo.getPath());
        //保存文件详细信息
        int result = resourcesFileDao.insert(resourcesFile);
        if (result > 0) {
            QueryWrapper<ResourcesFile> wrapper = new QueryWrapper();
            wrapper.eq("userid", resourcesFile.getUserid());
            Integer count = resourcesFileDao.selectCount(wrapper);
            Result upload = userCenterService.updateTitleByUserIdAndPoints(resourcesFile.getUserid(), count.doubleValue(), "UPLOAD");
            return Result.succeed("上传成功");
        } else {
            return Result.failed("上传失败");
        }
    }

    @Override
    public PageResult<ResourcesFile> selectAllResources(Map<String, Object> params) {
        try {
            Integer page = MapUtils.getInteger(params, PageUtil.PAGE);
            Integer limit = MapUtils.getInteger(params, PageUtil.LIMIT);
            page = page == null || page < 0 ? 0 : page;
            limit = limit == null || limit < 0 ? 10 : limit;
            PageHelper.startPage(page, limit);

            //查询自己上传的全部资源
            List<ResourcesFile> list = resourcesFileDao.selectAllResources(params);
            PageInfo<ResourcesFile> pageInfo = new PageInfo<>(list);
            return PageResult.<ResourcesFile>builder().code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result updateResource(ResourcesFile resourcesFile) {
        resourcesFile.setUpdtim(new Date());
        try {
            int result = resourcesFileDao.updateById(resourcesFile);
            if (result > 0) {
                return Result.succeed("修改成功");
            } else {
                return Result.failed("修改失败");
            }
        } catch (ServiceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult<ResourcesFileDto> selectAll(ResourcesFileDto resourcesFileDto) {
        try {
            Integer page = resourcesFileDto.getPage();
            Integer limit = resourcesFileDto.getLimit();
            page = page == null || page < 0 ? 0 : page;
            limit = limit == null || limit < 0 ? 10 : limit;
            PageHelper.startPage(page, limit);

            //查询自己上传的全部资源
            List<ResourcesFileDto> list = resourcesFileDao.selectAll(resourcesFileDto);
            PageInfo<ResourcesFileDto> pageInfo = new PageInfo<>(list);
            return PageResult.<ResourcesFileDto>builder().code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
